#include "StdAfx.h"
#include "ChatRoom.h"
#include <eXosip2/eXosip.h>
#include <osipparser2/osip_message.h>
#include "LinphoneCore.h"
#include "StaticManager.h"
CChatRoom::CChatRoom(CLinphoneCore *plc,char* to)
{
	lc= plc;
	char *real_url=NULL;
	osip_from_t *parsed_url=NULL;
	
	if (lc->linphone_core_interpret_url(to,&peer,&peer_url,&route)){
		lc->chatrooms=ms_list_append(lc->chatrooms,(void *)this);
	}
	
}

CChatRoom::~CChatRoom(void)
{
	lc->chatrooms=ms_list_remove(lc->chatrooms,(void *) this);
	osip_from_free(peer_url);
	ms_free(peer);
	ms_free(route);
}


void CChatRoom::sendMessage(const char *msg){
	const char *identity=lc->linphone_core_get_identity();
	osip_message_t *sip=NULL;
	eXosip_message_build_request(&sip,"MESSAGE",peer,identity,route);
	osip_message_set_content_type(sip,"text/plain");
	osip_message_set_body(sip,msg,strlen(msg));
	eXosip_message_send_request(sip);
}

bool CChatRoom::chatroomMatches(osip_from_t *from){
	if (peer_url->url->username && from->url->username && 
		strcmp(peer_url->url->username,from->url->username)==0) return TRUE;
	return FALSE;
}

void CChatRoom::messageReceived(const char *from, const char *msg){
	lc->vtable->linphone_gtk_text_received(lc, this, from, msg);
}



void CChatRoom::setUserdata(void * ud){
	user_data=ud;
}
void *CChatRoom::getUserdata(){
	return user_data;
}